package com.voxel.sense.infomsg.module.user.staff.details;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.app.BViewActivity;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.trace.map.CheckinsRecordActivity;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.utils.manager.QueryConfigManager;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.response.SearchMenuEntity;
import cyy.example.com.facotry.model.api.response.StaffDetailsEntity;
import cyy.example.com.facotry.model.api.response.StaffEntity;

/**
 * Created by ChenYiYao on 2018/8/3.
 */
public class StaffDetailsActivity extends BViewActivity<StaffDetailsContract.Presenter> implements StaffDetailsContract.View {

    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    @BindView(R.id.tv_real_name)
    TextView tv_real_name; //用户姓名
    @BindView(R.id.tv_user_name)
    TextView tv_user_name; //账户名
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_email)
    TextView tv_email;
    @BindView(R.id.tv_firm)
    TextView tv_firm;//所属单位
    @BindView(R.id.tv_department)
    TextView tv_department;//部门
    @BindView(R.id.tv_jobs)
    TextView tv_jobs;//岗位
    @BindView(R.id.tv_duty)
    TextView tv_duty;//职责
    @BindView(R.id.tv_superior)
    TextView tv_superior;//直属上级
    @BindView(R.id.tv_area)
    TextView tv_area;
    @BindView(R.id.tv_usert_type)
    TextView tv_usert_type;//用户类型
    @BindView(R.id.tv_role)
    TextView tv_role; //角色
    @BindView(R.id.iv_pic)
    ImageView iv_pic;//用户类型


    private StaffEntity.SysUserListBean datas;
    private String mUserId;

    /**
     * @param entity 传递用户实体
     * @param id     用户id titlebar
     */
    public static void show(Context context, StaffEntity.SysUserListBean entity, String id) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(IntentConstants.OBJECT_TYPE, entity);
        bundle.putString(IntentConstants.STRING_TYPE, id);
        Intent intent = new Intent(context, StaffDetailsActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_staff_details;
    }

    @Override
    protected StaffDetailsContract.Presenter initPresenter() {
        return new StaffDetailsPresenter(this);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        datas = (StaffEntity.SysUserListBean) bundle.getSerializable(IntentConstants.OBJECT_TYPE);
        mUserId = (String) bundle.getSerializable(IntentConstants.STRING_TYPE);
        if (TextUtils.isEmpty(mUserId)) {
            return false;
        }
        return super.initArgs(bundle);
    }

    @Override
    protected void initData() {
        super.initData();
        titlebar.setTitleText("用户列表");
        if (mUserId != null) {
            mPresenter.doLoadData(mUserId);
        }

        if (datas != null) {
            StaffEntity.SysUserListBean.SysUserDetailsBean sysUserDetails = datas.getSysUserDetails();
            if (sysUserDetails != null) {
                GlideUtils.GlideLoadPortrait(this, sysUserDetails.getHeadPhotoAddr(), iv_pic);
            }
        }
    }


    @Override
    protected void initEven() {
        super.initEven();
        titlebar.setRightTextAndLis("打卡记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckinsRecordActivity.show(StaffDetailsActivity.this, datas.getUserId(), datas.getUsername());
            }
        });
    }

    @Override
    public void onRefreshUI(StaffDetailsEntity entity) {

        StaffDetailsEntity.SysUserDetailsBean sysUserDetails = entity.getSysUserDetails();
        if (sysUserDetails != null) {
            GlideUtils.GlideLoadPortrait(Factory.app(), sysUserDetails.getHeadPhotoAddr(), iv_pic);
            tv_firm.setText(sysUserDetails.getInstitution());
            tv_department.setText(sysUserDetails.getDepartment());
            tv_jobs.setText(sysUserDetails.getPost());
            tv_duty.setText(sysUserDetails.getDuty());
        }

        StaffDetailsEntity.SysRoleBean sysRole = entity.getSysRole();
        if (sysRole != null) {
            String roleName = sysRole.getRoleName();
            tv_role.setText(roleName);
        }

        tv_user_name.setText(entity.getUsername());
        tv_real_name.setText(entity.getRealname());
        tv_phone.setText(entity.getPhone());

        //获取部门类型
        SearchMenuEntity.StaticSystemConstantDetailsBean dtType = QueryConfigManager.getInstance().getSpecialDeparmentType(entity.getUserType());
        if (dtType != null) {
            tv_usert_type.setText(dtType.getConstantName());
        }
        tv_email.setText(entity.getEmail());
        tv_superior.setText(entity.getPreuserName());

    }


}
